<template>
    <div class="wrapper">
        <div style="margin: 200px auto; background-color: #fff; width: 400px; height: 350px; padding:20px; border-radius: 10px;">
            <div style="margin: 20px 0; text-align: center; font-size: 24px;"><b>注 册</b></div>
            <el-form :model="user" :rules="rules" ref="userForm">
                <el-form-item prop="username">
                    <el-input placeholder="请输入账号" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" show-password v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item prop="confirmPassword">
                    <el-input placeholder="请确认密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" show-password v-model="user.confirm"></el-input>
                </el-form-item>
                
                <div style="margin: 5px 0; text-align: center;">
                    <el-button type="primary" size="medium" autocomplete="off" @click="login">注    册</el-button>
                    <el-button type="warning" size="medium" autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
                </div>
            </el-form>
            
        </div>
    </div>
</template>

<script>


import request from '@/utils/request'
export default {
   name:"LoginView",
   data(){
    return {
        user:{},
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
          ],
          confirm: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
          ]
        }
    }
   },
   methods:{
    login(){
        this.$refs['userForm'].validate((valid) => {
          if (valid) {
            if(this.user.password!==this.user.confirm){
                this.$message.error("两次输入的密码不一致")
                return false
            }

            // request.get("/user/username/"+this.user.username).then(res=>{
            //     if(res.data !=null){
            //         this.$message.error("用户名已存在")
            //         return false
            //     }
            // })
            
            
            request.post("/user/register",this.user).then(res=>{
                if(res.code==='200'){
                    this.$message.success("注册成功")
                }
            }) .catch(error => {
                console.error('Registration request error:', error);
                this.$message.error('注册失败，请重新输入新的用户名与密码。');
            });
          } 
        })
    }

   }
}
</script>

<style>
    .wrapper{
        height: 100vh;
        background-image: linear-gradient(to bottom right,#fc466b ,#3F5EFB);
        overflow: hidden;
    }
</style>